<?php
namespace app\common\model;

use plug\SUtil;

class OfflineRecord extends Base
{
    // 设置当前模型对应的完整数据表名称

    protected $table = 'ddt_offline_record';

    // 主键ID
    protected $pk = 'off_r_id';

//    插入线下代餐订单记录信息
    function InsertOfflineRecord($user,$orderid,$shop_id,$seat){
        $access_code = SUtil::random(8,1);
        $data = [
            'user_id' => $user['user_id'],
            'order_id' => $orderid,
            'shop_id' => $shop_id,
            'seat_id' => $seat['seat_id'],
            'seat_info' => json_encode($seat,true),
            'access_code' => $access_code,
            'add_time' => time(),
        ];
        $rs = $this->save($data);
        if($rs === false){
            $this->setError(-1,'插入信息操作失败');
            return false;
        }
        return true;
    }

    function getJoinOrder($where=[1], $item='*',$join = ['ddt_order o','o.order_id = or.order_id'])
    {
        $rs = $this
            ->alias('or')
            ->join($join)
            ->field($item)
            ->where($where)
            ->find();

        return $rs;
    }

    function EditRecord($params,$id){
        if($id>0){
            // 更新条件
            $where = ["{$this->pk}" => $id];
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // 更新数据
            $this->allowField(true)->save($params);
            $rs = $this->{$this->pk};
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }

}